{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 最简单的例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\softwares\\python\\lib\\site-packages\\umap\\distances.py:1063: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
      "  @numba.jit()\n",
      "d:\\softwares\\python\\lib\\site-packages\\umap\\distances.py:1071: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
      "  @numba.jit()\n",
      "d:\\softwares\\python\\lib\\site-packages\\umap\\distances.py:1086: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
      "  @numba.jit()\n",
      "d:\\softwares\\python\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "d:\\softwares\\python\\lib\\site-packages\\umap\\umap_.py:660: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n",
      "  @numba.jit()\n"
     ]
    }
   ],
   "source": [
    "from bertopic import BERTopic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 20个句子，一部分讨论天气，一部分讨论自然语言处理\n",
    "sentences = [\n",
    "    \"The weather today is sunny with clear skies.\",\n",
    "    \"Tomorrow's forecast predicts heavy rain and thunderstorms.\",\n",
    "    \"I love watching the sunset during mild weather evenings.\",\n",
    "    \"Natural language processing is a field of study in computer science.\",\n",
    "    \"NLP techniques are widely used in text analysis and language understanding.\",\n",
    "    \"Weather forecasting relies on complex algorithms and data analysis.\",\n",
    "    \"Understanding weather patterns is crucial for agriculture and disaster management.\",\n",
    "    \"NLP algorithms can help in sentiment analysis of social media posts.\",\n",
    "    \"The weather can be unpredictable, especially during transitional seasons.\",\n",
    "    \"NLP models like BERT and GPT-3 have revolutionized language understanding tasks.\",\n",
    "    \"Extreme weather events like hurricanes and tornadoes require advanced prediction models.\",\n",
    "    \"NLP is used in virtual assistants like Siri and Alexa to understand human commands.\",\n",
    "    \"Climate change is affecting global weather patterns.\",\n",
    "    \"NLP can aid in machine translation, making communication across languages easier.\",\n",
    "    \"Weather satellites provide real-time data for meteorologists to analyze.\",\n",
    "    \"Semantic analysis is an important aspect of natural language processing.\",\n",
    "    \"Weather phenomena such as El Niño impact weather worldwide.\",\n",
    "    \"Part of speech tagging is a fundamental task in NLP.\",\n",
    "    \"The weather in coastal areas is often influenced by ocean currents.\",\n",
    "    \"NLP helps in chatbots to generate human-like responses.\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Topic</th>\n",
       "      <th>Count</th>\n",
       "      <th>Name</th>\n",
       "      <th>Representation</th>\n",
       "      <th>Representative_Docs</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>0_weather_the_and_is</td>\n",
       "      <td>[weather, the, and, is, during, for, patterns,...</td>\n",
       "      <td>[The weather today is sunny with clear skies.,...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>1_nlp_in_of_language</td>\n",
       "      <td>[nlp, in, of, language, is, analysis, and, nat...</td>\n",
       "      <td>[Natural language processing is a field of stu...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Topic  Count                  Name  \\\n",
       "0      0     11  0_weather_the_and_is   \n",
       "1      1      9  1_nlp_in_of_language   \n",
       "\n",
       "                                      Representation  \\\n",
       "0  [weather, the, and, is, during, for, patterns,...   \n",
       "1  [nlp, in, of, language, is, analysis, and, nat...   \n",
       "\n",
       "                                 Representative_Docs  \n",
       "0  [The weather today is sunny with clear skies.,...  \n",
       "1  [Natural language processing is a field of stu...  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topic_model = BERTopic(min_topic_size=2) # 创建模型\n",
    "topic_model.fit_transform(sentences) # 拟合模型\n",
    "topic_model.get_topic_info() # 获取主题聚类信息"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
